package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.ValueSet;
import com.innovations.inn.service.IValueSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 字典明细Controller
 *
 * @author dp
 * @date 2024-11-01
 */
@Api(tags = "字典明细")
@RestController
@RequestMapping("/inn/value/set")
public class ValueSetController extends BaseController {
    @Autowired
    private IValueSetService valueSetService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询字典明细列表
     */
    @ApiOperation("查询字典明细列表")
    @PreAuthorize("@ss.hasPermi('inn:set:list')")
    @GetMapping("/list")
    public TableDataInfo list(ValueSet valueSet) {
        startPage();
        List<ValueSet> list = valueSetService.selectValueSetList(valueSet);
        return getDataTable(list);
    }

    /**
     * 导出字典明细列表
     */
    @PreAuthorize("@ss.hasPermi('inn:set:export')")
    @Log(title = "字典明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ValueSet valueSet) {
        List<ValueSet> list = valueSetService.selectValueSetList(valueSet);
        ExcelUtil<ValueSet> util = new ExcelUtil<ValueSet>(ValueSet.class);
        util.exportExcel(response, list, "字典明细数据");
    }

    /**
     * 获取字典明细详细信息
     */
    @ApiOperation("获取字典明细详细信息")
    @PreAuthorize("@ss.hasPermi('inn:set:query')")
    @GetMapping(value = "/{valueId}")
    public AjaxResult getInfo(@PathVariable("valueId") Long valueId) {
        return success(valueSetService.selectValueSetByValueId(valueId));
    }

    /**
     * 新增字典明细
     */
    @ApiOperation("新增字典明细")
    @PreAuthorize("@ss.hasPermi('inn:set:add')")
    @Log(title = "字典明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody ValueSet valueSet) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        valueSetService.completeData(valueSet, userId, 1);
        return toAjax(valueSetService.insertValueSet(valueSet));
    }

    /**
     * 修改字典明细
     */
    @ApiOperation("修改字典明细")
    @PreAuthorize("@ss.hasPermi('inn:set:edit')")
    @Log(title = "字典明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody ValueSet valueSet) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        valueSetService.completeData(valueSet, userId, 2);
        return toAjax(valueSetService.updateValueSet(valueSet));
    }

    /**
     * 删除字典明细
     */
    @ApiOperation("删除字典明细")
    @PreAuthorize("@ss.hasPermi('inn:set:remove')")
    @Log(title = "字典明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{valueIds}")
    public AjaxResult remove(@PathVariable Long[] valueIds) {
        return toAjax(valueSetService.deleteValueSetByValueIds(valueIds));
    }
}
